#!/usr/bin/python3
# -*- coding: utf-8 -*-
'''
@File    :  cre_conn_to_onenet.py
@Time    :  2022/05/02 19:36:26
@Author  :  HHLiang
@Contact :  sternapeg@qq.com
@Version :  1.0
@Desc    :  EDP协议建立连接
'''

from config import config_run
from edp_sdk import socket, gen_conn_msg


if __name__ == '__main__':

    # 以下设备和产品信息，请自行补全
    # api_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'    # 设备APIkey
    # product_id = 'XXXXXX'                       # 产品ID
    # device_id = 'XXXXXXXXX'                     # 设备ID
    # auth_info = 'XXXXXXXXXXXX'                  # 鉴权信息

    config = config_run('A1')

    api_key     = config[0]
    product_id  = config[1]
    device_id   = config[2]
    auth_info   = config[3]

    host        = config[4]
    port        = config[5]

    # 创建Sockey连接, IPv4的TCP连接
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # 建立连接
    s.connect((host, port))

    # 发送登录报文， 并接收响应
    conn_msg = gen_conn_msg(pid=product_id, auth_info=auth_info)
    print('发送报文数据：', conn_msg)
    s.send(conn_msg)
    # 接收响应并打印
    resp = s.recv(1024)
    print('响应结果：', resp)
